﻿(function ($) {
    $.mutilB = {
        _lookupB: { currentGroup: "", suffix: "", $target: null, pk: "id", mutilBox: null },
        _util: {
            _lookupBPrefix: function (key) {
                var strDot = _lookupB.currentGroup ? "." : "";
                return _lookupB.currentGroup + strDot + key + _lookupB.suffix;
            },
            lookupPk: function (key) {
                return this._lookupBPrefix(key);
            },
            lookupField: function (key) {
                return this.lookupPk(key);
            }
        },
        lookupB: function () {
            //return this.each(function () {
                var $this = $(this), options = {
                    mask: true,
                    width: $this.attr('width') || 820, height: $this.attr('height') || 400,
                    maxable: eval($this.attr("maxable") || "true"),
                    resizable: eval($this.attr("resizable") || "true")
                };                
                $this.click(function (event) {
                    _lookupB = $.extend(_lookupB, {
                        currentGroup: $this.attr("lookupGroup") || "",
                        suffix: $this.attr("suffix") || "",
                        $target: $this,
                        pk: $this.attr("lookupPk") || "id"
                    });
                    var url = ($this.attr("href")).replaceTmById2($(event.target).parents(".unitBox:first"));
                    if (!url.isFinishedTm()) {
                        alertMsg.error($this.attr("warn") || DWZ.msg("alertSelectMsg"));
                        return false;
                    }
                    var rel = $this.attr("rel") || "_blank";
                    $.pdialog.open(url, rel, $this.attr("title") || $this.text(), options);
                    return false;
                });
            //});
        },
        multLookupB: function () {
            return this.each(function () {
                var $this = $(this), args = {};
                $this.click(function (event) {
                    var $unitBox = $this.parents(".unitBox:first");
                    $unitBox.find("[name='" + $this.attr("multLookupB") + "']").filter(":checked").each(function () {
                        var _args = DWZ.jsonEval($(this).val());
                        for (var key in _args) {
                            var value = args[key] ? args[key] + "," : "";
                            args[key] = value + _args[key];
                        }
                    });

                    if ($.isEmptyObject(args)) {
                        alertMsg.error($this.attr("warn") || DWZ.msg("alertSelectMsg"));
                        return false;
                    }
                    this.bringBack(args);
                });
            });
        },
        bringBackSuggest: function (args) {
            // <li><span>hhhhhhhhh1</span><a class="closebutton"></a></li>
            var $box = _lookup['$target'].find(":ul");
            if ($box.length < 1) $box = $("<ul></ul>").appendTo(_lookup['$target']);

            $box.find(":input").each(function () {
                var $input = $(this), inputName = $input.attr("name");

                for (var key in args) {
                    var name = (_lookup.pk == key) ? _util.lookupPk(key) : _util.lookupField(key);

                    if (name == inputName) {
                        $input.val(args[key]);
                        if ($input.hasClass("toMutilB"))
                            this.toMutilB($input);
                        break;
                    }
                }
            });
        },
        bringBack: function (args) {
            this.bringBackSuggest(args);
            $.pdialog.closeCurrent();
        },
        toMutilB: function ($input) {
            var valstr = $input.val();
            if (valstr) {
                var valarry = valstr.split(',');
                var inputname = $input.attr("name");
                var boxname = inputname.replace(".", "_") + "spanToMutilColsB";
                var $box = $("<span id='" + boxname + "'></span>");
                _lookupB['mutilBox'] = $box;
                $("#" + boxname).remove();
                for (var i = 0; i < valarry.length; i++) {
                    var name = inputname.replace(".", "[" + i + "].");
                    var $target = $("<input type='hidden' id='" + name + "' name='" + name + "' value='" + valarry[i] + "'/>");
                    $target.appendTo($box);
                }
                $input.after($box);
            }
        },
        removeB: function () {
            $(this).parent().remove();
            var $t = $.mutilB._lookupB['mutilBox'];
            if ($t && $t.length > 0) $t.remove();
        },
        init: function () {
            $(".multiB").each(function () {
                $.mutilB.lookupB();
                //$.mutilB.multLookupB();
                //alert();
                $(this).find(".closebutton").click(function () {
                    $(this).parent().remove();
                });
            });
        }
    }
})(jQuery);